package com.dtw.ditouwang.ui.coin

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.telecom.Call
import com.dtw.ditouwang.R
import com.dtw.ditouwang.beans.CommonBean
import com.dtw.ditouwang.beans.OrderListBean
import com.dtw.ditouwang.repositories.UserRepository
import com.dtw.ditouwang.ui.BaseActivity
import com.dtw.ditouwang.ui.OnItemClickListener
import kotlinx.android.synthetic.main.activity_coin.*
import retrofit2.Callback
import retrofit2.Response

class OrderListActivity : BaseActivity(), OnItemClickListener {
    var orderList = ArrayList<OrderListBean.DataBean>()
    var orderAdapter = OrderAdapter(orderList)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order_list)
        title = "已对换"
        enableBack()

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = orderAdapter
        orderAdapter.onItemClickListener = this
        getOrderList()
    }

    fun getOrderList() {
        webApi.getOrderList(UserRepository.getInstance().userID, 0).enqueue(object : Callback<OrderListBean> {
            override fun onFailure(call: retrofit2.Call<OrderListBean>, t: Throwable) {
            }

            override fun onResponse(call: retrofit2.Call<OrderListBean>, response: Response<OrderListBean>) {
                var datas = response.body()
                if (datas != null) {
                    orderList.clear()
                    orderList.addAll(datas.data)
                    orderAdapter.notifyDataSetChanged()
                }
            }
        })
    }

    override fun onItemClick(resID: Int, position: Int, state: Int) {
        webApi.confirmTranstion(orderList[position].id).enqueue(object : Callback<CommonBean> {
            override fun onFailure(call: retrofit2.Call<CommonBean>, t: Throwable) {
            }

            override fun onResponse(call: retrofit2.Call<CommonBean>, response: Response<CommonBean>) {
                var commonBean = response.body()
                if (commonBean == null) {
                    showToast("服务器异常")
                    return
                }
                if (!commonBean.isResult) {
                    showToast(commonBean.msg)
                    return
                }
                getOrderList()
            }
        })
    }
}